<div id="{{action}}-quick_pay-{{block_id}}" style="margin-top:10px;text-align:center;"></div>
<script>
  $(function () {
    moi.createScript("https://test-api.star-saas.com/v1/pub/iframejs/quickpay/quickPayPalBtn.v1.min.js", function () {
      if (!QuickPayPalBtn) {
        console.error('QuickPayPalBtn is not defined');
        return;
      }
      const paypalBtn = new QuickPayPalBtn();
      paypalBtn.mount('#{{action}}-quick_pay-{{block_id}}');
      paypalBtn.createBtn({
        env: 'uat'
      });
      if (paypalBtn.button) {
        paypalBtn.button.onclick = async function (event) {
          event.stopPropagation();
          event.preventDefault();
          const { code, data } = await moi.ecBuyNow({
            ec_block_id: "{{block_id}}",
            ec_action_from: "{{action}}",
            ec_payment_id: "{{ec_payment.payment_id}}",
          }, false);
          if (code) {
            return
          }
          const items = data.products.map(item => `${item.name}#,#${item.sku}#,#${item.price}#,#${item.quantity}`).join('#;#');
          const unitPrice = data.products.reduce((total, item) => {
            return (total + Number(item.price) * item.quantity);
          }, 0).toFixed(2);
          const order_no = Date.now().toString();
          
          try {
            const encryptionRes = await fetch('/homeapi/checkouts/payment/starsaasecpay/createEncryption', {
              method: 'POST',
              headers: {
                'Content-Type': 'application/json'
              },
              body: JSON.stringify({
                order_no: order_no,
                payment_id: "{{ec_payment.payment_id}}",
                currency: data.currency_code,
                amount: unitPrice,
                return_url: data.return_url
              })
            });
            
            const encryptionData = await encryptionRes.json();
            
            if (encryptionData.code !== 0) {
              moi.alertInfo({ content: encryptionData.msg || '获取加密数据失败' });
              return;
            }
            
            const params = {
              merchant_id: '{{ec_payment.interface_type_param.merchant_id}}',
              account_id: '{{ec_payment.interface_type_param.account_id}}',
              order_no: order_no,
              currency: data.currency_code,
              amount: unitPrice,
              note: '',
              payment_method: 'Paypal_Quick', 
              return_url: data.return_url,
              notify_url: data.notify_url,
              cancel_url: data.cancel_url,
              default_lang: '',
              shopper_email: '',
              shopper_level: '',
              shopper_phone: '',
              billing_country: '',
              items: items,
              quick_jump_mode: '{{ec_payment.interface_type_param.quick_jump_mode}}', 
              encryption_data: encryptionData.data.encryption_data,
              domain: "{{ec_payment.interface_type_param.domain}}"
            };
            
            // 调用
            paypalBtn.confirmPayment(params,function(data){
              if(!data.status){
                moi.alertInfo({ content: data.msg || 'error' });
              }
            });
          } catch (error) {
            moi.alertInfo({ content: 'error' });
          }
        };
      }

    });
  })


</script>